// 设置倒计时的初始时间（以秒为单位）
// let timeLeft = 120 * 60; // 例如，10分钟
function startCountdown() {
    // 获取显示倒计时的元素
    const countdownElement = document.getElementById('countdown');

    // 创建一个函数来更新倒计时
    function updateCountdown() {
        // 计算分钟和秒
        const minutes = Math.floor(timeLeft / 60);
        const seconds = timeLeft % 60;

        // 格式化时间，确保秒数为两位数
        const formattedTime = `${minutes < 10 ? '0' : ''}${minutes}分钟${seconds < 10 ? '0' : ''}${seconds}秒`;

        // 更新页面上的倒计时文本
        countdownElement.textContent = formattedTime;

        // 检查倒计时是否结束
        if (timeLeft <= 0) {
            clearInterval(countdownInterval);
            countdownElement.textContent = "时间到！";
        } else {
            // 每秒减少1秒
            timeLeft--;
        }
    }

    // 每秒更新一次倒计时
    const countdownInterval = setInterval(updateCountdown, 1000);
}
// 当页面加载完成时，启动倒计时
window.onload = startCountdown;
//////////////////////////////////////////////////////////////////////////////////////////////
// var data = {
//     "title": "在线试卷",
//     "exam": [
//         {
//             "title": "单项选择题",
//             "infos": null,
//             "values": [
//                 {
//                     "type": 1,
//                     "questionStem": "梅奥的人际关系学说属于",
//                     "options": [
//                         "行为科学理论",
//                         "科学管理理论",
//                         "一般管理理论",
//                         "权变管理理论"
//                     ],
//                     "answer": [
//                         "A"
//                     ],
//                     "analysis": null
//                 }
//             ]
//         },
//         {
//             "title": "多项选择题",
//             "infos": null,
//             "values": [
//                 {
//                     "type": 2,
//                     "questionStem": "下列属于高层管理者的有",
//                     "options": [
//                         "董事长",
//                         "首席执行官",
//                         "总经理",
//                         "项目经理",
//                         "车间主任"
//                     ],
//                     "answer": [
//                         "A",
//                         "B",
//                         "C"
//                     ],
//                     "analysis": null
//                 }
//             ]
//         },
//         {
//             "title": "判断题",
//             "infos": null,
//             "values": [
//                 {
//                     "type": 3,
//                     "questionStem": "组织文化反映一个组织的精神风貌，决定羞镪织荫在凝聚力的大小",
//                     "options": [
//                         ""
//                     ],
//                     "answer": [
//                         "1"
//                     ],
//                     "analysis": null
//                 }
//             ]
//         }
//     ]
// };
function test(test_id) {
    if(test_id!=""){
        //$.ajax({
            //type : 'POST',
            //url : 'data/exam.json',
            //data : {'testId':test_id},
            //dataType : "json",
            //success : function(data){
            console.log(data);
            var titleB = data.title;
            var exam = data.exam;
            var test_box = '';

            $.each(exam, function(h, exam) {
                var title = exam.title;
                var info = exam.infos!=null?'<h4 class="jxz-title">'+exam.infos+'</h4>':'';
                var test = exam.values;
                var topic_box = '';

                $.each(test, function(i, topic) {//1单选2多选3判断4填空5问答8论述题6完型填空7阅读理解
                    var type = topic.type;
                    var options = topic.options;
                    var answer = topic.answer;
                    var analysis = topic.analysis==null||topic.analysis==""?"无":topic.analysis;
                    var option_box = '';//题目

                    if(type==1){
                        //answer_op = '';
                        $.each(options, function(j, option) {
                            var op = convert(j);
                            option_box += `
                                    <div class="jxz-option radio">
                                        <label>
                                            <input name="test`+h+''+i+`" type="radio" value="`+op+`"> `+op+`：`+option+`
                                        </label>
                                    </div>
                                    `;
                        });

                    }else if(type==2){
                        //answer_op = '';
                        $.each(options, function(j, option) {
                            var op = convert(j);
                            option_box += `
                                    <div class="jxz-option checkbox">
                                        <label>
                                            <input name="test`+h+''+i+`" type="checkbox" value="`+op+`"> `+op+`：`+option+`
                                        </label>
                                    </div>
                                    `;
                        });


                    }else if(type==3){

                        option_box = `
                                <div class="jxz-option radio">
                                    <label>
                                        <input name="test`+h+''+i+`" type="radio" value="1"> 正确
                                    </label>
                                </div>
                                <div class="jxz-option radio">
                                    <label>
                                        <input name="test`+h+''+i+`" type="radio" value="0"> 错误
                                    </label>
                                </div>
                                `;
                    }else if(type==4){

                        option_box += `
                                <div class="jxz-option">
                                    <textarea name="test`+h+''+i+`" class="form-control" style="width: 50%" rows="5" placeholder="答案以空格隔开"></textarea>
                                </div>
                                `;
                    }else if(type==5||type==8){

                        option_box += `
                                <div class="jxz-option">
                                    <textarea name="test5" class="form-control" style="width: 50%" rows="5" placeholder=""></textarea>
                                </div>
                                `;
                    }else if(type==6){

                        $.each(options, function(j, option) {
                            var op = convert(j);
                            option_box += `
                                    <div class="jxz-option radio-inline">
                                        <label>
                                            `+op+`：`+option+`
                                        </label>
                                    </div>
                                    `;
                        });
                        option_box += `
                                <div class="jxz-option">
                                    <textarea name="test`+h+''+i+`" class="form-control" style="width: 50%" rows="5" placeholder="答案以空格隔开"></textarea>
                                </div>`;
                    }else if(type==7){

                        $.each(options, function(j, option) {
                            var op = convert(j);
                            option_box += `
                                    <div class="jxz-option radio">
                                        <label>
                                            <input name="test`+h+''+i+`" type="radio" value="`+op+`"> `+op+`：`+option+`
                                        </label>
                                    </div>
                                    `;
                        });
                    }

                    var answer_op = '';//答案
                    if(type==3){
                        $.each(answer, function(i, aw) {
                            answer_op += aw==1 ? "正确" : "错误";
                        });
                    }else{
                        $.each(answer, function(i, aw) {
                            answer_op += answer.length==(i+1) ? aw : aw+" ";
                        });
                    }
                    topic_box += `
                            <div class="testCon" data-type="`+type+`" data-answer="`+answer_op+`">
                                <h4 class="jxz-title">`+topic.questionStem+`</h4>
                                `+option_box+`
                                <div class="topic-answer">
                                   <p>正确答案：`+answer_op+`</p>
                                </div>
                            </div>
                            `;

                });
                test_box += `
                            <div class="jxz-box col-md-12">
                            <h4 class="tesTitle">`+title+`</h4>
                            `+info+`
                            `+topic_box+`
                        </div>
                        `;
            });

            var test_html = `
                    <div class="page-header">
                        <h3 class="text-center">`+titleB+`</h3>
                        <p style="text-align:right">考试剩余时间：<span id="countdown" style="color:red">10:00</span></p>
                    </div>
                    <form class="" id="testForm">
                        <div class="test-form-box">
                            `+test_box+`
                        </div>
                        <div class="form-group assignment">
                            <button type="button" class="btn btn-primary" onclick="assignment()">交卷</button>
                        </div>
                    </form>`;
            $('#testArea').html(test_html)
            //},
            //error:function(data){
                //alert("网络异常，请稍后重试");
            //}
        //});
    }else{
        alert("试题获取失败！");
    }
}
//交卷
function assignment(){
    $(".testCon h4").css("color","#555");
    var _temp_tip = "yes";
    var tall = 0;
    var total=0;
    $(".testCon").each(function(i){
        var type = $(this).attr("data-type");
        if(type==2){
            if($(this).find('input[type="checkbox"]:checked').val() == undefined){
                _temp_tip = "no";
                $(this).find("h4").css("color","#00B895");
            }
            total++;
        }else if(type==1||type==3||type==7){
            if($(this).find('input[type="radio"]:checked').val() == undefined){
                _temp_tip = "no";
                $(this).find("h4").css("color","#00B895");
            }
            total++;
        }else if(type==4||type==5||type==8||type==6){
            if($.trim($(this).find('textarea').val()) == ''){
                _temp_tip = "no";
                $(this).find("h4").css("color","#00B895");
            }
        }
        tall++;
    });

    if(_temp_tip == "no"){
        alert("还有题目没做完");
        return;
    }

    var err = 0;
    $(".testCon").each(function(i){
        var type = $(this).attr("data-type");
        var aw = $(this).attr("data-answer");
        var set_answer = '';

        if(type==2){

            var ckAarray = $(this).find('input[type="checkbox"]:checked');
            var ans = '';
            ckAarray.each(function (i,item) {
                ans += ckAarray.length==i+1?item.value:item.value+" ";
            });
            if(ans != aw){
                $(this).find("h4").css("color","red");
                err++;
            }
            set_answer = ans;

        }else if(type==1){

            var rd = $(this).find('input[type="radio"]:checked').val();
            if(rd != aw){
                $(this).find("h4").css("color","red");
                err++;
            }
            set_answer = rd;

        }else if(type==3){

            var rpd = $(this).find('input[type="radio"]:checked').val();
            rpd = rpd==0 ? "错误" : (rpd==1 ? "正确" : "");
            if(rpd != aw){
                $(this).find("h4").css("color","red");
                err++;
            }
            set_answer = rpd;

        }else if(type==4||type==5||type==8||type==6){

            var textVal = $.trim($(this).find('textarea').val());
            if(textVal!= aw){
                $(this).find("h4").css("color","red");
                err++;
            }
            set_answer = textVal;
        }
        $(this).find("span.userAnswer").text(set_answer);
    });
    let fenshu=parseInt((total-err)*100/total);
    alert("本次考试得分："+fenshu);
    $(".topic-answer").show();
    insert(fenshu);
}
//数字转换成大写字母
function convert(num){
    num = num + 1;
    return num <= 26 ? String.fromCharCode(num + 64) : convert(~~((num - 1) / 26)) + convert(num % 26 || 26);
}

//添加成绩信息到数据库
function insert(chengji){
    let score={
        userId:student.userId,
        eid:data.eid,
        score:chengji
    };
    $.post("score/add",score,function(resp){
        if (resp==200){
            alert(resp.msg);
        }else{
            alert(resp.msg);
        }
    },"json")
}